<template>
  <div>
    <header>
      选择商品分类
      <el-cascader
        v-model="valueList"
        :options="options"
        @change="handleChange"
        :props="props"
      ></el-cascader>
    </header>
    <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="动态参数" name="many">
        <el-button type="primary" :disabled="disabled" @click="add"
          >添加参数</el-button
        >
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="expand" prop="">
            <template slot-scope="scope">
              <el-tag
                :key="tag"
                v-for="tag in scope.row.attr_vals"
                closable
                :disable-transitions="false"
                @close="handleClose(scope.row, tag)"
              >
                {{ tag }}
              </el-tag>
              <el-input
                class="input-new-tag"
                v-if="scope.row.inputVisible"
                v-model="scope.row.inputValue"
                ref="saveTagInput"
                size="small"
                @keyup.enter.native="handleInputConfirm(scope.row)"
                @blur="handleInputConfirm(scope.row)"
              >
              </el-input>
              <el-button
                v-else
                class="button-new-tag"
                size="small"
                @click="showInput(scope.row)"
                >+ New Tag</el-button
              >
            </template>
          </el-table-column>
          <el-table-column type="index" label="#" prop=""></el-table-column>
          <el-table-column label="参数名称" prop="attr_name"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="edit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="del(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="静态属性" name="only">
        <el-button type="primary" :disabled="disabled" @click="add"
          >添加参数</el-button
        >
        <el-table :data="tableData2" style="width: 100%">
          <el-table-column type="expand" prop="">
            <template slot-scope="scope">
              <el-tag
                :key="tag"
                v-for="tag in scope.row.attr_vals"
                closable
                :disable-transitions="false"
                @close="handleClose(scope.row, tag)"
              >
                {{ tag }}
              </el-tag>
              <el-input
                class="input-new-tag"
                v-if="scope.row.inputVisible"
                v-model="scope.row.inputValue"
                ref="saveTagInput"
                size="small"
                @keyup.enter.native="handleInputConfirm(scope.row)"
                @blur="handleInputConfirm(scope.row)"
              >
              </el-input>
              <el-button
                v-else
                class="button-new-tag"
                size="small"
                @click="showInput(scope.row)"
                >+ New Tag</el-button
              >
            </template>
          </el-table-column>
          <el-table-column type="index" label="#" prop=""></el-table-column>
          <el-table-column label="参数名称" prop="attr_name"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="edit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="del(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <!-- 添加提示 -->
     <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
                 
      <el-input v-model="attr_name" placeholder="请输入内容"></el-input>        
         
      <span slot="footer" class="dialog-footer">
                       
        <el-button @click="dialogVisible = false">取 消</el-button>            
            <el-button type="primary" @click="addPush">确 定</el-button>        
           
      </span>
             
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategories,
  getCategories2,
  addCategories,
  delCategories,
  editCategories,
} from "../utils/api";
export default {
  data() {
    return {
      valueList: [],
      options: [],
      tableData: [],
      tableData2: [],
      disabled: false,
      editId: "",
      activeName: "many",
      errVal: 0,
      attr_name: "",
      dialogVisible: false,
      editId: "",
      props: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "click",
        checkStrictly: true,
      },
    };
  },
  computed: {},
  created() {
    this.render();
  },
  mounted() {},
  watch: {},
  methods: {
    //   添加弹出框
    add() {
      this.dialogVisible = true;
    },
    // 编辑弹出框
    edit(index, row) {
      console.log("编辑开始");
      this.dialogVisible = true;
      this.attr_name = row.attr_name;
      this.editId = row.attr_id;
      this.render();
    },
    addPush() {
      if (this.editId) {
        editCategories({
          id: this.errVal,
          attrId: this.editId,
          attr_name: this.attr_name,
          attr_sel: this.activeName,
        }).then((res) => {
          console.log("编辑成功");
          this.render();
        });
      } else {
        addCategories({
          id: this.errVal,
          attr_name: this.attr_name,
          attr_sel: this.activeName,
        }).then((res) => {
          if (this.activeName == "many") {
            console.log(res);
            this.tableData.push(res.data);
            this.dialogVisible = false;
            this.attr_name = "";
            (this.editId = ""), this.render();
          } else {
            if (this.activeName == "only") {
              console.log(res);
              this.tableData2.push(res.data);
              this.dialogVisible = false;
              this.attr_name = "";
              (this.editId = ""), this.render();
            }
          }
        });
      }
    },
    handleClick() {
      console.log(this.activeName);
      getCategories2({ id: this.valueList[2], sel: this.activeName }).then(
        (res) => {
          console.log(res.data);
          let deepTable = JSON.parse(JSON.stringify(res.data));
          deepTable.forEach((item) => {
            item.attr_vals = item.attr_vals ? item.attr_vals.split(",") : [];
            item.inputValue = "";
            item.inputVisible = false;
          });
          this.tableData = deepTable;
        }
      );
    },
    handleChange(val) {
      this.errVal = val[2];
      console.log(val);
      if (val.length == 3) {
        if (this.activeName == "many") {
          getCategories({ id: val[2], sel: this.activeName }).then((res) => {
            console.log(res);
            let deepTable = JSON.parse(JSON.stringify(res.data));
            deepTable.forEach((item) => {
              item.attr_vals = item.attr_vals ? item.attr_vals.split(",") : [];
              item.inputValue = "";
              item.inputVisible = false;
            });
            this.tableData = deepTable;
          });
        } else {
          getCategories2({ id: val[2], sel: this.activeName }).then((res) => {
            console.log(res);
            let deepTable = JSON.parse(JSON.stringify(res.data));
            deepTable.forEach((item) => {
              item.attr_vals = item.attr_vals ? item.attr_vals.split(",") : [];
              item.inputValue = "";
              item.inputVisible = false;
            });
            this.tableData2 = deepTable;
          });
        }
      } else {
        this.$message({
          type: "warning", // success error warning
          message: "只允许三级分类设置参数！",
        });
        this.valueList = [];
        return false;
      }
    },
    render() {
      getCategories().then((res) => {
        console.log(res);
        this.options = res.data;
        console.log(this.options);
      });
    },
    del(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      });
      delCategories({ id: row.cat_id, attrId: row.attr_id }).then((res) => {
        this.tableData.splice(index, 1);
      });
      this.render();
    },

    handleClose(row, index) {
      row.attr_vals.splice(row.attr_vals.indexOf(index), 1);
    },

    showInput(row) {
      row.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm(row) {
      let inputValue = row.inputValue;
      if (inputValue) {
        row.attr_vals.push(inputValue);
      }
      row.inputVisible = false;
      row.inputValue = "";
    },
  },
  components: {},
};
</script>

<style scoped lang="scss">
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
